package com.lepu.app.fun.my.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@SuppressWarnings("serial")
public class BeanMsgBbs implements Serializable{
	private static final String TABLE_NAME = "MsgBbs";
	
	public int id; 
    public String MessageID;  
    public String ThreadID ;   
    public String Abstract; 
    public String HoldUserID; 
    public String BeReplyUserID; 
    public String BeReplyUserNickname; 
    public String ReplyUserID;  
    public String ReplyUserNickname;  
    public String ReplyContent;  
    public int RecordCreateTime;  
    
    public static ArrayList<BeanMsgBbs> parseJsonData(String dataString) {
        String result = "";
        try {
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONArray obj2 = (JSONArray) obj1.get("ListInfo");

            result = obj2.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        ArrayList<BeanMsgBbs> testArraylist = gson.fromJson(result,
        		new TypeToken<List<BeanMsgBbs>>() {}.getType());
        return testArraylist;
    }
    
    //==============================================================
    //添加
    public static boolean insertMyMessage(BeanMsgBbs item) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("MessageID", item.MessageID);
        values.put("ThreadID", item.ThreadID);
        values.put("Abstract", item.Abstract);
        values.put("HoldUserID", item.HoldUserID);
        values.put("BeReplyUserID", item.BeReplyUserID);
        values.put("BeReplyUserNickname", item.BeReplyUserNickname);
        values.put("ReplyUserID", item.ReplyUserID);
        values.put("ReplyUserNickname", item.ReplyUserNickname);
        values.put("ReplyContent", item.ReplyContent);
        values.put("RecordCreateTime", item.RecordCreateTime);

        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }

    //删除某一条记录
    public static boolean deleteItem(String UserID, String msgId) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "BeReplyUserID=? and MessageID=?",new String[]{UserID, msgId});

        return flag;
    }

    //删除所有
    public static boolean deleteAllItem(String UserID) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "BeReplyUserID=?", new String[]{UserID});

        return flag;
    }

    //获取所有数据
    public static ArrayList<BeanMsgBbs> getMyMessage(String UserID) {
    	ArrayList<BeanMsgBbs> provinces = new ArrayList<BeanMsgBbs>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] { 
					"id","MessageID", "ThreadID", "Abstract", "HoldUserID", "BeReplyUserID", 
					"BeReplyUserNickname","ReplyUserID","ReplyUserNickname","ReplyContent", "RecordCreateTime"}, 
					"BeReplyUserID=?",new String[] {UserID}, null, null,"RecordCreateTime desc");

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }

    /*
     * 当前用户未读数据
     */
    public static int getNewMessageCount(String LoginUserID, int board) {

        int count = 0;
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return count;
        String sql = "select count(*) from MsgSystem where userId=? and targetType=? and isRead=0";
        String result = dbHelp.executeScalar(sql, new String[]{LoginUserID, String.valueOf(board)});
        count = UtilityBase.parseInt(result);

        return count;
    }

    public static boolean updateNewMessageRead(String LoginUserID, int board) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("IsRead", 1);

        flag = dbHelp
                .update(TABLE_NAME, values, "LoginUserID=? and Board=?", new String[]{
                        LoginUserID, String.valueOf(board)});

        return flag;
    }

	/**
	 * 解析cursor转换为对象
	 */
    
	private static void cursorParse(ArrayList<BeanMsgBbs> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanMsgBbs beanMsg = new BeanMsgBbs();
			beanMsg.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanMsg.MessageID = (cursor.getString(cursor.getColumnIndex("MessageID")));
			beanMsg.ThreadID = (cursor.getString(cursor.getColumnIndex("ThreadID")));
			beanMsg.Abstract = (cursor.getString(cursor.getColumnIndex("Abstract")));
			beanMsg.HoldUserID = (cursor.getString(cursor.getColumnIndex("HoldUserID")));
			beanMsg.BeReplyUserID = (cursor.getString(cursor.getColumnIndex("BeReplyUserID")));
			beanMsg.BeReplyUserNickname = (cursor.getString(cursor.getColumnIndex("BeReplyUserNickname")));
			beanMsg.ReplyUserID = (cursor.getString(cursor.getColumnIndex("ReplyUserID")));
			beanMsg.ReplyUserNickname = (cursor.getString(cursor.getColumnIndex("ReplyUserNickname")));
			beanMsg.ReplyContent = (cursor.getString(cursor.getColumnIndex("ReplyContent")));
			beanMsg.RecordCreateTime = (cursor.getInt(cursor.getColumnIndex("RecordCreateTime")));
			msgArray.add(beanMsg);
		}
	}
	
}
